﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace AcademiaInteligenteData.DAL
{
    class DaoConnection
    {
        public void Executar(string comando)
        {
            Executar(comando, false);
        }

        public void Executar(string comando, bool procedure)
        {
            Executar(comando, procedure, null);
        }

        public void Executar(string comando, bool procedure, List<SqlParameter> parametros)
        {
            SqlConnection con = new SqlConnection(GetStringConnection());
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(comando, con);
                if (procedure)
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                foreach (var item in parametros)
                {
                    cmd.Parameters.Add(item);
                }
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public DataSet Selecionar(string comando)
        {
            return Selecionar(comando, false);
        }
        public DataSet Selecionar(string comando, bool procedure)
        {
            return Selecionar(comando, procedure, null);
        }
        public DataSet Selecionar(string comando, bool procedure, List<SqlParameter> parametros)
        {
            SqlConnection con = new SqlConnection(GetStringConnection());
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(comando, con);
                if (procedure)
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                foreach (var item in parametros)
                {
                    cmd.Parameters.Add(item);
                }
                DataSet ds = new DataSet();
                new SqlDataAdapter(cmd).Fill(ds);
                return ds;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }
        private string GetStringConnection()
        {
            return @"Data Source=DEYVISON-PC\FSDIGITAL;Initial Catalog=academiaInteligente;Integrated Security=True";
        }
    }
}
